package com.lemon.ai.socket.packets.spiritgame.model.generator;

import java.util.List;

import com.lemon.ai.socket.packets.spiritgame.model.db.MetaData;
import com.lemon.ai.socket.packets.spiritgame.model.dto.Table;
import com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringUtils;

public class FactoryGenerator extends AbstractGenerator {

	private String GET_METHODS = 
			"\r" + 
					"	public CLASS_NAMEDAO getCLASS_NAMEDAO() {\r" +
					"		return new CLASS_NAMEDAOImpl();\r" +
					"	}\r";

	public FactoryGenerator() {
		super("FactoryTemp.txt");
	}

	public String generate(List<Table> tables) {
		String temp = generatePropertiesAndMethods(tables);
		return super.temp.
				replace("IMPORTS", temp).
				replace("PACKAGE_NAME", super.prop.getProperty("package")).
				replace("GET_METHODS", generateDaoGetMethods(tables));
	}

	/**
	 * 生成导入语句
	 * @param tables
	 * @return
	 */
	private String generatePropertiesAndMethods(List<Table> tables) {
		//		String s = "";
		//		for (Table table : tables) {
		//			s += "import PACKAGE_NAME.dao.Impl." + StringUtils.generateClassName(table.getName()) + "DAOImpl;\r";
		//			s += "import PACKAGE_NAME.dao." + StringUtils.generateClassName(table.getName()) + "DAO;\r";
		//		}
		//		return s;

		String s = "";
		s += "import PACKAGE_NAME.dao.Impl.*;\r";
		s += "import PACKAGE_NAME.dao.*;\r";
		return s;
	}

	public String generateFileName() {
		return super.prop.getProperty("dir")
				+ super.prop.getProperty("package").replace(".", "\\")
				+ "\\dao\\factory\\" + "DaoFactory.java";
	}

	/**
	 * 根据所有的Table，生成Factory中的getDao方法
	 * @param tables
	 * @return	GET_METHODS
	 */
	public String generateDaoGetMethods(List<Table> tables) {
		String getMethods = "";
		for (int i = 0; i < tables.size(); i++) {
			Table t = tables.get(i);
			getMethods += GET_METHODS;
			getMethods = 
					getMethods.
					replace( "CLASS_NAME", StringUtils.generateClassName(t.getName())).
					replace( "CLASS_PARAMETER", StringUtils.generateParameterName(t.getName()));
		}
		return getMethods;
	}

	public static void main(String[] args) {
		MetaData meta = MetaData.getInstance();
		List<Table> tables = meta.getTables();
		FactoryGenerator g = new FactoryGenerator();
		System.out.println(g.generate(tables));
	}

}
